package com.dsp.soundscape.server.rpc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.dsp.soundscape.client.services.SoundscapeManagerService;
import com.dsp.soundscape.model.AlbumInfo;
import com.dsp.soundscape.server.jsf.SoundscapeManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class SoundscapeManagerServiceImpl extends RemoteServiceServlet implements SoundscapeManagerService {
	private static final long serialVersionUID = 5560989490625188828L;

	public SoundscapeManager getSoundscapeManager() {
		return (SoundscapeManager) getThreadLocalRequest().getSession().getAttribute("SoundscapeManager");
	}
	
	@Override
	public List<AlbumInfo> getAlbumsByArtist(String artist) {
		List<AlbumInfo> result = getSoundscapeManager().getAlbumsByArtist(artist);
		Collections.sort (result);
		return result;
	}

	@Override
	public List<AlbumInfo> getAlbumsByYear(String year) {
		List<AlbumInfo> result = getSoundscapeManager().getAlbumsByYear(year); 
		Collections.sort (result);
		return result;
	}

	@Override
	public List<AlbumInfo> randomize() {
		return getSoundscapeManager().randomize();
	}

//	@Override
//	public List<AlbumInfo> getAllAlbums() {
//		return getSoundscapeManager().getAlbums();
//	}

	@Override
	public List<String> getArtists() {
		List<String> artists = new ArrayList<String>();
		artists.addAll(getSoundscapeManager().getArtists());
		Collections.sort(artists);
		return artists;
	}

	@Override
	public List<String> getYears() {
		List<String> years = new ArrayList<String>();
		years.addAll(getSoundscapeManager().getYears());
		Collections.sort(years, new Comparator<String>() {

			@Override
			public int compare (String o1, String o2) {
				return (-1 * o1.compareTo(o2));
			}
			
		});
		return years;
	}

	@Override
	public AlbumInfo getInfo(String id) {
		return getSoundscapeManager().getAlbumInfo(id);
	}
	
}
